package com.woniuxy.cloud.carl.order.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.cloud.carl.order.client.ServiceQueryClient;
import com.woniuxy.cloud.carl.order.client.param.FindServiceByTypeParam;
import com.woniuxy.cloud.carl.order.client.result.FirstTypeDto;
import com.woniuxy.cloud.carl.order.client.result.SecondTypeDto;
import com.woniuxy.cloud.carl.order.client.result.ServiceDetailDto;
import com.woniuxy.cloud.carl.order.client.result.ThirdTypeDto;
import com.woniuxy.cloud.carl.order.infra.model.*;
import com.woniuxy.cloud.carl.order.service.*;
import com.woniuxy.cloud.carl.user.client.UserCarClient;
import com.woniuxy.cloud.carl.user.client.result.UserCarDto;
import com.woniuxy.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@RestController
public class ServiceQueryApiImpl implements ServiceQueryClient {

    @Resource
    private ServiceService serviceeService;
    @Resource
    private CarServiceService carService;
    @Resource
    private ServiceSpuService spuService;
    @Autowired
    FirstTypeService firstTypeService;
    @Autowired
    SecondTypeService secondTypeService;
    @Autowired
    ThirdTypeService thirdTypeService;
    @Autowired
    UserCarClient userCarClient;
    @Autowired
    CarServiceService carServiceService;

    @Override
    public Result<ServiceDetailDto> findServiceDetail(Long serviceId) {
        Servicee service = serviceeService.getById(serviceId);
        ServiceDetailDto serviceDetailDto = BeanUtil.toBean(service, ServiceDetailDto.class); //保证服务对应的skuId 只有一个
        ServiceSpu serviceSpu = spuService.getOne(new QueryWrapper<ServiceSpu>().eq("service_id", serviceId));
        if(serviceSpu!=null){
            serviceDetailDto.setSkuId(serviceSpu.getSkuId());
            serviceDetailDto.setSkuCount(serviceSpu.getSkuCount());
        }
        List<CarService> cars = carService.list(new QueryWrapper<CarService>().eq("service_id", serviceId));
        if(cars!=null){
            List<Long> carIds = cars.stream().map(CarService::getCarId).collect(Collectors.toList());
            serviceDetailDto.setCarList(carIds);
        }
        return Result.success(serviceDetailDto);
    }

    @Override
    public Result<List<FirstTypeDto>> findFirstType() {
        List<FirstType> list = firstTypeService.list();
        ArrayList<FirstTypeDto> firstTypeDtos = new ArrayList<>();
        for (FirstType firstType : list) {
            FirstTypeDto firstTypeDto = BeanUtil.copyProperties(firstType, FirstTypeDto.class);
            firstTypeDto.setTypeSelf(1);
            firstTypeDtos.add(firstTypeDto);
        }
        return Result.success(firstTypeDtos);
    }

    @Override
    public Result<List<SecondTypeDto>> findSecond(Long firstTypeId) {
        List<SecondType> list = secondTypeService.list(new QueryWrapper<SecondType>()
                .eq("first_type_id",firstTypeId));
        ArrayList<SecondTypeDto> secondTypeDtos = new ArrayList<>();
        for (SecondType secondType : list) {
            SecondTypeDto secondTypeDto = BeanUtil.copyProperties(secondType, SecondTypeDto.class);
            secondTypeDto.setTypeSelf(2);
            secondTypeDtos.add(secondTypeDto);
        }
        return Result.success(secondTypeDtos);
    }

    @Override
    public Result<List<ThirdTypeDto>> findThird(Long thirdTypeId) {
        List<ThirdType> list = thirdTypeService.list(new QueryWrapper<ThirdType>()
                .eq("second_type_id",thirdTypeId));
        ArrayList<ThirdTypeDto> thirdTypeDtos = new ArrayList<>();
        for (ThirdType thirdType : list) {
            ThirdTypeDto thirdTypeDto = BeanUtil.copyProperties(thirdType, ThirdTypeDto.class);
            thirdTypeDto.setTypeSelf(3);
            thirdTypeDtos.add(thirdTypeDto);
        }
        return Result.success(thirdTypeDtos);
    }

    @Override
    public Result<List<ServiceDetailDto>> findServiceByType(FindServiceByTypeParam param) {
        //查询当前绑定车辆
        List<Long> longList=null;
        if (param.getUserId()!=null){
            Result<UserCarDto> defaultCar = userCarClient.findDefaultCar(param.getUserId());
            UserCarDto carDto = defaultCar.getData();
            List<CarService> carServices = carServiceService.list(new QueryWrapper<CarService>()
                    .eq("car_id", carDto.getCarId()));
            longList = carServices.stream().map(o -> o.getServiceId()).collect(Collectors.toList());
        }


        ArrayList<ServiceDetailDto> serviceDetailDtos = new ArrayList<>();
        List<Servicee> list = serviceeService.list(new QueryWrapper<Servicee>()
                .eq("type_flag", param.getTypeSelf()).eq("type_id", param.getTypeId()));
        for (Servicee servicee : list) {
            ServiceDetailDto serviceDetailDto = BeanUtil.copyProperties(servicee, ServiceDetailDto.class);
            if (longList!=null){
                if (longList.contains(servicee.getServiceId()))
                    serviceDetailDtos.add(serviceDetailDto);
            }else {
                serviceDetailDtos.add(serviceDetailDto);
            }
        }
        return Result.success(serviceDetailDtos);
    }
}
